<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import {ref} from "vue";
import {ElMessage} from "element-plus";

/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.materialInfo,
  routerPre: 'materialInfo',
  params: {
    name: '',
  },
  format: (list) => {
    return list.map(item => {
      let unit = item.unit
      if (item.unit == 1) {
        unit = '元/克'
      } else if (item.unit == 2) {
        unit = '元/千克'
      } else if (item.unit == 3) {
        unit = '元/吨'
      }
      return {
        ...item,
        unit
      }
    })
  }
})

const changeStatus = (row) => {
  http.materialInfo.updateStatus({
    id: row.id,
    recommend: row.recommend
  }).then(res => {
    ElMessage.success('操作成功')
  }).catch(() => {
    if (row.recommend === 1) {
      row.recommend = 0
    } else {
      row.recommend = 1
    }
  })
}
</script>

<template>
  <PageMain title="原料信息">
    <el-form label-suffix="：" inline>
      <el-form-item label="原料名称">
        <el-input clearable v-model="searchParams.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="原料名称" prop="name"></el-table-column>
        <el-table-column label="原料价格" prop="price">
          <template #default="scope">
            <span>{{scope.row.price}}{{scope.row.unit}}</span>
          </template>
        </el-table-column>
        <el-table-column label="基于该原料生产的产品" property="purpose"/>
        <el-table-column label="产地" property="producing"/>
        <el-table-column label="龙头企业" property="leadingEnterprise"/>
        <el-table-column label="推荐" property="recommend">
          <template #default="scope">
            <el-switch :active-value="0" :inactive-value="1" v-model="scope.row.recommend" @change="() => changeStatus(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180px" property="createTime"></el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
